<?php

namespace App\Http\Controllers;

use App\Models\News;
use App\Models\Person;
use App\Models\Quotation;
use App\Models\Support;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class MainController extends Controller
{
    public function NewsLists(Request $request): JsonResponse
    {
        return $this->success(News::where('status', 1)
            ->when($request->input('title'), function ($query, $title) {
                return $query->where('title', 'like', "%$title%");
            })
            ->orderBy('id', 'desc')
            ->paginate($request->input('limit', 10)));
    }

    public function NewsCreate(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'title' => 'required',
                'image' => 'required',
                'content' => 'required',
            ], [
                'title.required' => '请输入标题',
                'image.required' => '请上传图片',
                'content.required' => '请输入内容',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        $res = News::query()->updateOrCreate(['id' => $request->input('id')], [
            'title' => $request->input('title'),
            'image' => $request->input('image'),
            'content' => $request->input('content'),
            'description' => $request->input('description'),
            'status' => 1
        ]);
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function NewsDetail(Request $request): JsonResponse
    {
        return $this->success(News::find($request->input('id')));
    }

    public function NewsDelete(Request $request): JsonResponse
    {
        $res = News::where('id', $request->input('id'))->delete();
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function SupportLists(Request $request): JsonResponse
    {
        return $this->success(Support::where('id', '<>', 4)->orderByDesc('id')->paginate($request->input('limit', 10)));
    }

    public function supportAll(Request $request): JsonResponse
    {
        return $this->success(Support::where('id', '<>', 4)->orderByDesc('id')->get());
    }

    public function SupportGood(Request $request): JsonResponse
    {
        return $this->success(Support::find(4));
    }

    public function SupportCreateNew(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'tag' => 'required',
                // 'image' => 'required',
                'content' => 'required',
            ], [
                'tag.required' => '请输入标题',
                // 'image.required' => '请上传图片',
                'content.required' => '请输入内容',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        if ($request->input('id') && $request->input('id') != 4 && !$request->input('image')) {
            return $this->error('请上传图片');
        }
        $res = Support::query()->updateOrCreate(['id' => $request->input('id')], [
            'tag' => $request->input('tag'),
            'image' => $request->input('image'),
            'content' => $request->input('content'),
        ]);
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function SupportDelete(Request $request): JsonResponse
    {
        $res = Support::where('id', $request->input('id'))->where('id', '<>', 4)->delete();
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function SupportCreate(Request $request): JsonResponse
    {
        if ($request->input('a')) {
            Support::updateOrCreate([
                'tag' => 'a'
            ], [
                'content' => $request->input('a'),
            ]);
        }
        if ($request->input('b')) {
            Support::updateOrCreate([
                'tag' => 'b'
            ], [
                'content' => $request->input('b'),
            ]);
        }
        if ($request->input('c')) {
            Support::updateOrCreate([
                'tag' => 'c'
            ], [
                'content' => $request->input('c'),
            ]);
        }
        if ($request->input('d')) {
            Support::updateOrCreate([
                'tag' => 'd'
            ], [
                'content' => $request->input('d'),
            ]);
        }
        if ($request->input('e')) {
            Support::updateOrCreate([
                'tag' => 'e'
            ], [
                'content' => $request->input('e'),
            ]);
        }
        if ($request->input('f')) {
            Support::updateOrCreate([
                'tag' => 'f'
            ], [
                'content' => $request->input('f'),
            ]);
        }
        if ($request->input('g')) {
            Support::updateOrCreate([
                'tag' => 'g'
            ], [
                'content' => $request->input('g'),
            ]);
        }
        if ($request->input('h')) {
            Support::updateOrCreate([
                'tag' => 'h'
            ], [
                'content' => $request->input('h'),
            ]);
        }
        if ($request->input('i')) {
            Support::updateOrCreate([
                'tag' => 'i'
            ], [
                'content' => $request->input('i'),
            ]);
        }
        return $this->success();
    }

    public function QuotationLists(Request $request): JsonResponse
    {
        return $this->success(Quotation::with(['person'])->orderBy('id', 'desc')->paginate($request->input('limit', 10)));
    }

    public function QuotationMine(Request $request): JsonResponse
    {
        return $this->success(Quotation::where('person_id', $request->input('person_id'))->orderBy('id', 'desc')->paginate($request->input('limit', 10)));
    }

    public function QuotationCreate(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'person_id' => 'required',
                'name' => 'required',
            ], [
                'person_id.required' => '请登录',
                'name.required' => '请输入项目名称',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        $data = [
            'person_id' => $request->input('person_id'),
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            'industry' => $request->input('industry'),
            'contact' => $request->input('contact'),
            'phone' => $request->input('phone'),
            'contact_business' => $request->input('contact_business'),
            'phone_business' => $request->input('phone_business'),
            'material' => $request->input('material'),
            'pressure' => $request->input('pressure'),
            'brand' => $request->input('brand'),
            'kongyaji' => json_encode($request->input('kongyaji')),
            'chuqiguan' => json_encode($request->input('chuqiguan')),
            'lengganji' => json_encode($request->input('lengganji')),
            'ganzaoji' => json_encode($request->input('ganzaoji')),
            'golvqi' => json_encode($request->input('golvqi')),
            'zengyaji' => json_encode($request->input('zengyaji')),
            'qita' => json_encode($request->input('qita')),
            'range' => implode(',', $request->input('range')),
            'remark' => $request->input('remark'),
            'images' => implode(',', $request->input('images')),
            'videos' => implode(',', $request->input('videos')),
            'status' => 0,
            'note' => '',
            'industry_reason' => $request->input('industry') == 4 ? $request->input('industry_reason') : '',
        ];
        $res = Quotation::updateOrCreate(['id' => $request->input('id')], $data);
        return $res === false ? $this->error('操作失败') : $this->success();
    }

    public function personList(Request $request): JsonResponse
    {
        return $this->success(Person::when($request->input('phone'), function ($query, $phone) {
            return $query->where('phone', 'like', "%$phone%");
        })->orderBy('id', 'desc')->paginate($request->input('limit', 10)));
    }

    public function QuotationEditAdmin(Request $request): JsonResponse
    {
        try {
            $this->validate($request, [
                'name' => 'required',
            ], [
                'name.required' => '请输入项目名称',
            ]);
        } catch (ValidationException $e) {
            return $this->error($e->validator->errors()->first());
        }
        $data = [
            'person_id' => $request->input('person_id'),
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            'industry' => $request->input('industry'),
            'contact' => $request->input('contact'),
            'phone' => $request->input('phone'),
            'contact_business' => $request->input('contact_business'),
            'phone_business' => $request->input('phone_business'),
            'material' => $request->input('material'),
            'pressure' => $request->input('pressure'),
            'brand' => $request->input('brand'),
            'kongyaji' => json_encode($request->input('kongyaji')),
            'chuqiguan' => json_encode($request->input('chuqiguan')),
            'lengganji' => json_encode($request->input('lengganji')),
            'ganzaoji' => json_encode($request->input('ganzaoji')),
            'golvqi' => json_encode($request->input('golvqi')),
            'zengyaji' => json_encode($request->input('zengyaji')),
            'qita' => json_encode($request->input('qita')),
            'range' => implode(',', $request->input('range')),
            'remark' => $request->input('remark'),
            'images' => implode(',', $request->input('images')),
            'videos' => implode(',', $request->input('videos')),
            'status' => 0,
            'note' => $request->input('note'),
            'industry_reason' => $request->input('industry') == 4 ? $request->input('industry_reason') : '',
        ];
        $res = Quotation::updateOrCreate(['id' => $request->input('id')], $data);
        return $res === false ? $this->error('操作失败') : $this->success();
    }
}
